package per.sunmes.cl.g.gsys;

import per.sunmes.cl.g.screen.GameScreen;
import per.sunmes.cl.g.screen.TitleScreen;
import per.sunmes.cl.g.sys.G;
import per.sunmes.cl.g.sys.U;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.utils.Align;

public class GameActor extends Group {

	Group uiGroup;
	Group gameGroup;
	Label debugLabel;

	public GameActor() {
		setTransform(false);
		setSize(G.stage.getWidth(), G.stage.getHeight());

		G.gameActor = this;
		// /////////////
		uiGroup = new Group();
		uiGroup.setTransform(false);
		gameGroup = new Group();
		// //////
		debugLabel = new Label("FPS:--", new LabelStyle(U.defaultFont(),
				Color.WHITE));
		debugLabel.setPosition(20, 20);
		debugLabel.addAction(new Action() {
			public boolean act(float delta) {
				debugLabel.setText(String.format("FPS:%d\nFrameRate:%d",
						Gdx.graphics.getFramesPerSecond(), G.frameRate));
				return false;
			}
		});
		// /////
		gameGroup.addActor(new TitleScreen());
		// ///////
		addActor(gameGroup);
		addActor(uiGroup);
		addActor(debugLabel);

		G.stage.addListener(new InputListener() {
			@Override
			public boolean keyDown(InputEvent event, int keycode) {
				if (keycode == Keys.ESCAPE) {
					startDemoGame();
					return true;
				}
				return false;
			}
		});
	}

	public void startDemoGame() {
		gameGroup.clear();
		gameGroup.addActor(new GameScreen());
	}

	public Label showMsg(String msg) {
		return showMsg(msg, 1.5f);
	}

	Label oldMsgLabel;

	public Label showMsg(String msg, float delta) {
		if (oldMsgLabel != null
				&& (oldMsgLabel.isVisible() || oldMsgLabel.getParent() != null)) {
			oldMsgLabel.remove();
		}
		Label l = new Label(msg, new LabelStyle(U.getCardNameFont(),
				Color.WHITE));
		Image bg = U.createMask();
		bg.setSize(getWidth(), l.getPrefHeight() + 60);
		bg.setColor(Color.BLACK);
		addActor(bg);
		U.centerParent(bg, this);

		addActor(l);
		l.setFontScale(2);
		l.setAlignment(Align.center);
		l.setPosition(getWidth() / 2, getHeight() / 2, Align.center);
		l.addAction(Actions.sequence(Actions.delay(delta),
				Actions.fadeOut(0.3f), Actions.removeActor()));
		bg.addAction(Actions.sequence(Actions.delay(delta),
				Actions.fadeOut(0.3f), Actions.removeActor()));
		oldMsgLabel = l;

		return l;
	}

}
